package com.yuelan.apartment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yuelan.apartment.common.Constant;
import com.yuelan.apartment.domain.ApaRoom;
import com.yuelan.apartment.domain.vo.ApaRoomVo;
import com.yuelan.apartment.mapper.ApaRoomMapper;
import com.yuelan.apartment.service.ApaRoomService;
import com.yuelan.common.exception.GlobalException;
import com.yuelan.common.exception.ServiceException;
import com.yuelan.common.utils.DateUtils;
import com.yuelan.common.utils.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;

/**
 * @description:
 * @Author: ZhaoYi
 * @date: 24-5-23 9:21
 */
@Service
public class ApaRoomServiceImpl implements ApaRoomService {

    @Resource
    private ApaRoomMapper apaRoomMapper;

    private static final Logger logger = LoggerFactory.getLogger(GlobalException.class);


    @Transactional
    @Override
    public int addRoom(ApaRoom apaRoom) {
        if (apaRoom == null) {
            throw new ServiceException(Constant.SERVICE_ERROR);
        }
        //房间是否已存在
        LambdaQueryWrapper<ApaRoom> apaRoomWrapper = new LambdaQueryWrapper<>();
        apaRoomWrapper.eq(ApaRoom::getApartmentId, apaRoom.getApartmentId())
                .eq(ApaRoom::getRoomNum, apaRoom.getRoomNum());
        boolean existsRoom = apaRoomMapper.exists(apaRoomWrapper);
        if (!existsRoom) {
            throw new ServiceException(Constant.ROOM_OUT);
        }
        // 新增房间
        apaRoom.setCreateTime(DateUtils.getNowDate());
        apaRoom.setUpdateTime(DateUtils.getNowDate());
        apaRoom.setCreateBy(SecurityUtils.getUsername());
        apaRoom.setUpdateBy(SecurityUtils.getUsername());
        return apaRoomMapper.insert(apaRoom);
    }


    @Override
    public List<ApaRoomVo> pageList(ApaRoom apaRoom) {
        return apaRoomMapper.selectRoomPage(SecurityUtils.getUsername(), apaRoom);
    }


    @Override
    public List<ApaRoomVo> export() {
        return apaRoomMapper.export(SecurityUtils.getUsername());
    }


    @Override
    public int deleteRoomIds(Long[] ids) {
        if (ids == null) {
            throw new ServiceException(Constant.SERVICE_ERROR);
        }
        return apaRoomMapper.deleteRoomByIds(ids);
    }


    @Transactional
    @Override
    public void updateRoomState(Long[] ids) {
        if (ids == null) {
            throw new ServiceException(Constant.SERVICE_ERROR);
        }
        apaRoomMapper.updateRoomStateByTenantId(ids);
    }


    @Override
    public int update(ApaRoom apaRoom) {
        if (apaRoom == null) {
            throw new ServiceException(Constant.SERVICE_ERROR);
        }
        LambdaUpdateWrapper<ApaRoom> apaRoomWrapper = new LambdaUpdateWrapper<>();
        apaRoomWrapper.setEntity(apaRoom);
        return apaRoomMapper.update(apaRoomWrapper);
    }


    @Override
    public ApaRoom load(Long id){
        if (id == null) {
            throw new ServiceException(Constant.SERVICE_ERROR);
        }
        LambdaQueryWrapper<ApaRoom> apaRoomWrapper = new LambdaQueryWrapper<>();
        apaRoomWrapper.eq(ApaRoom::getId, id);
        return apaRoomMapper.selectById(apaRoomWrapper);
    }

}
